package com.yxw.springmvc.userBehavior.service.impl;

import com.yxw.springmvc.userBehavior.config.AppConfig;
import com.yxw.springmvc.userBehavior.enums.UserPwdStatEnum;
import com.yxw.springmvc.userBehavior.enums.UserStatEnum;
import com.yxw.springmvc.userBehavior.file.FileOperService;
import com.yxw.springmvc.userBehavior.model.RandomCode;
import com.yxw.springmvc.userBehavior.service.RandomCodeService;
import com.yxw.springmvc.userBehavior.mapper.UserMapper;
import com.yxw.springmvc.userBehavior.mapper.UserPwdMapper;
import com.yxw.springmvc.userBehavior.model.User;
import com.yxw.springmvc.userBehavior.model.UserPwd;
import com.yxw.springmvc.userBehavior.service.UserPwdService;
import com.yxw.springmvc.userBehavior.service.UserService;
import com.yxw.springmvc.userBehavior.util.CollectionUtils;
import com.yxw.springmvc.userBehavior.util.PasswordUtil;
import com.yxw.springmvc.userBehavior.util.SequenceUtil;
import com.yxw.springmvc.userBehavior.validator.RandomCodeValidatorService;
import com.yxw.springmvc.userBehavior.validator.UserPwdValidatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * Created by hp on 2016/9/8.
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserPwdMapper userPwdMapper;

    @Resource
    private AppConfig appConfig;

    @Resource
    private SequenceUtil sequenceUtil;

    @Resource
    private RandomCodeService randomCodeService;

    @Resource
    private RandomCodeValidatorService randomCodeValidatorService;

    @Resource
    private UserPwdValidatorService userPwdValidatorService;

    @Resource
    private UserPwdService userPwdService;

    @Resource
    private FileOperService fileOperService;

    @Override
    public User findUserInfo() {
        return userMapper.findUserInfo();
    }

    @Override
    public User register(String userName,MultipartFile file) {
        if(CollectionUtils.isEmpty(userName)){
            return null;
        }
        User user = new User();
        user.setUserName(userName);
        String code = sequenceUtil.getSequence();
        user.setUserCode(code);
        user.setStat(UserStatEnum.USER_VALID.getCode());
        userMapper.insert(user);
        UserPwd pwd = new UserPwd();
        pwd.setUserCode(code);
        pwd.setUserPwd(PasswordUtil.genMD5Password(code,appConfig.getSYS_INIT_PWD()));
        pwd.setStat(UserPwdStatEnum.USER_PWD_VALID.getCode());
        userPwdMapper.insert(pwd);
        //把用户上传的图片上传到文件服务器
        fileOperService.upload(file);

        return user;
    }

    @Override
    public User login(String userCode,String pwd,String randomCode) {

        if(CollectionUtils.isEmpty(userCode) || CollectionUtils.isEmpty(pwd) || CollectionUtils.isEmpty(randomCode)){
            return null;
        }
        User user = userMapper.selectUserByUserCode(userCode);
        if(CollectionUtils.isEmpty(user)){
            return null;
        }
        UserPwd userPwd = userPwdService.selectPwdByUserCode(userCode);
        if(CollectionUtils.isEmpty(userPwd)){
            return null;
        }
        String randomId = randomCode.substring(0, 16);
        String randomNum = randomCode.substring(16);
        RandomCode code = randomCodeService.selectById(randomId);
        if(!randomCodeValidatorService.checkRandomCodeIsValid(code,randomNum)){
            return null;
        }
        if(!userPwdValidatorService.checkUserPwd(userCode,pwd,randomCode,userPwd)){
            return null;
        }
        return user;
    }
}
